package cobolinfo;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import sun.java2d.Disposer;

public class CalendarForm extends JDialog implements ActionListener {
	// variable
	private static String auxiliad_dia, anox, mesx, diax, separador;
	public static String fecha_elegida;
	private static int dia_entero, xx, yy;
	public static boolean cerrar;
	// otras cosas
	static String[] ARRMES = { "Enero", "Febrero", "Marzo", "Abril", "Mayo",
			"Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre",
			"Diciembre" };
	static String[] ARRDIASSEMANA = { "Lun", "Mar", "Mie", "Jue", "Vie",
			"Sab","Dom" };
	static Calendar currentDate;
	static JLabel meslabel;
	private String mDateIn = "";
	private String mDateEnd = "";
	private javax.swing.JTextField mTextField;

	public void cerrarMetodo() {
		this.dispose();
	}

	JButton anomenos;
	JButton mesmenos;
	JButton mesmas;
	JButton anomas;
	JButton auxponer;
	JLabel label_2;
	JLabel label_3;
	JLabel label_4;
	JLabel label_5;
	JLabel label_6;
	JLabel label_7;
	JLabel label_8;
	int MesAnterior;
	int MesSiguiente;
	int AnoAnterior;
	int AnoSiguiente;
	int AnoAnteriorAno;
	int AnoSiguienteAno;
	int a;
	static int mes;

	// botones de dia
	private static JButton dia1;
	private static JButton dia2;
	private static JButton dia3;
	private static JButton dia4;
	private static JButton dia5;
	private static JButton dia6;
	private static JButton dia7;
	private static JButton dia8;
	private static JButton dia9;
	private static JButton dia10;
	private static JButton dia11;
	private static JButton dia12;
	private static JButton dia13;
	private static JButton dia14;
	private static JButton dia15;
	private static JButton dia16;
	private static JButton dia17;
	private static JButton dia18;
	private static JButton dia19;
	private static JButton dia20;
	private static JButton dia21;
	private static JButton dia22;
	private static JButton dia23;
	private static JButton dia24;
	private static JButton dia25;
	private static JButton dia26;
	private static JButton dia27;
	private static JButton dia28;
	private static JButton dia29;
	private static JButton dia30;
	private static JButton dia31;

	public CalendarForm(String separador_aux, javax.swing.JTextField textField) {
		this.mTextField = textField;
		cerrar = false;
		separador = separador_aux;
		// botones
		dia1 = new JButton("01");
		dia2 = new JButton("02");
		dia3 = new JButton("03");
		dia4 = new JButton("04");
		dia5 = new JButton("05");
		dia6 = new JButton("06");
		dia7 = new JButton("07");
		dia8 = new JButton("08");
		dia9 = new JButton("09");
		dia10 = new JButton("10");
		dia11 = new JButton("11");
		dia12 = new JButton("12");
		dia13 = new JButton("13");
		dia14 = new JButton("14");
		dia15 = new JButton("15");
		dia16 = new JButton("16");
		dia17 = new JButton("17");
		dia18 = new JButton("18");
		dia19 = new JButton("19");
		dia20 = new JButton("20");
		dia21 = new JButton("21");
		dia22 = new JButton("22");
		dia23 = new JButton("23");
		dia24 = new JButton("24");
		dia25 = new JButton("25");
		dia26 = new JButton("26");
		dia27 = new JButton("27");
		dia28 = new JButton("28");
		dia29 = new JButton("29");
		dia30 = new JButton("30");
		dia31 = new JButton("31");
		// eventos de los botones
		dia1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("01");
			}
		});
		dia2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("02");
			}
		});
		dia3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("03");
			}
		});
		dia4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("04");
			}
		});
		dia5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("05");
			}
		});
		dia6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("06");
			}
		});
		dia7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("07");
			}
		});
		dia8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("08");
			}
		});
		dia9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("09");
			}
		});
		dia10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("10");
			}
		});
		dia11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("11");
			}
		});
		dia12.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("12");
			}
		});
		dia13.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("13");
			}
		});
		dia14.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("14");
			}
		});
		dia15.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("15");
			}
		});
		dia16.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("16");
			}
		});
		dia17.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("17");
			}
		});
		dia18.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("18");
			}
		});
		dia19.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("19");
			}
		});
		dia20.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("20");
			}
		});
		dia21.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("21");
			}
		});
		dia22.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("22");
			}
		});
		dia23.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("23");
			}
		});
		dia24.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("24");
			}
		});
		dia25.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("25");
			}
		});
		dia26.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("26");
			}
		});
		dia27.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("27");
			}
		});
		dia28.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("28");
			}
		});
		dia29.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("29");
			}
		});
		dia30.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("30");
			}
		});
		dia31.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				elijeDia("31");
			}
		});

		// calendario

		calendarioLayout customLayout = new calendarioLayout();
		currentDate = Calendar.getInstance();
		setFont(new Font("Helvetica", Font.PLAIN, 12));
		setLayout(customLayout);

		anomenos = new JButton("<<");
		add(anomenos);
		anomenos.addActionListener(this);

		mesmenos = new JButton("<");
		add(mesmenos);
		mesmenos.addActionListener(this);

		meslabel = new JLabel("");
		meslabel.setFont(new Font("Dialog", 1, 10));
		add(meslabel);

		mesmas = new JButton(">");
		add(mesmas);
		mesmas.addActionListener(this);

		anomas = new JButton(">>");
		add(anomas);
		anomas.addActionListener(this);

		label_2 = new JLabel("Lun");
		add(label_2);

		label_3 = new JLabel("Mar");
		add(label_3);

		label_4 = new JLabel("Mie");
		add(label_4);

		label_5 = new JLabel("Jue");
		add(label_5);

		label_6 = new JLabel("Vie");
		add(label_6);

		label_7 = new JLabel("Dom");
		add(label_7);

		label_8 = new JLabel("Sab");
		add(label_8);

		// entra a la clase maldita
		mostrar_calendario();

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {

				// System.exit(0);
			}
		});
	}

	// metodo que ejecuta el cambiar de fecha
	private void elijeDia(String dia) {
		diax = dia;
		mesx = String.valueOf(currentDate.get(currentDate.MONTH));
		anox = String.valueOf(currentDate.get(currentDate.YEAR));
		int aux_mes = Integer.parseInt(mesx);
		aux_mes += 1;
		mesx = String.valueOf(aux_mes);
		if (aux_mes < 10) {
			mesx = "0" + mesx;
		}
		fecha_elegida = diax + separador + mesx + separador + anox;
		this.mTextField.setText(fecha_elegida);
		/*
		 * if (this.mIsEnd == true) { this.mDateEnd = fecha_elegida; } else {
		 * this.mDateIn = fecha_elegida; }
		 */
		// cerramos esta clase
		cerrarMetodo();
	}

	public String GetDateIn() {
		System.out.println("IN:" + this.mDateIn);
		return this.mDateIn;
	}

	public String GetDateEnd() {
		System.out.println("END:" + this.mDateEnd);
		return this.mDateEnd;
	}

	public static void calendar() {

	}

	public void mostrar_calendario() {
		String aux_dia = "";
		int b = 0;
		int c = 1;
		int tmp;
		int x = 50;
		int y = 130;
		int dia = currentDate.get(currentDate.DATE);
		mes = currentDate.get(currentDate.MONTH);
		mes++;
		int ano = currentDate.get(currentDate.YEAR);
		int TotalDiasMes = currentDate
				.getActualMaximum(currentDate.DAY_OF_MONTH);
		if (dia > currentDate.get(currentDate.DAY_OF_WEEK))
			tmp = dia - currentDate.get(currentDate.DAY_OF_WEEK);
		else
			tmp = currentDate.get(currentDate.DAY_OF_WEEK) - dia;

		while (tmp > 7) {
			tmp = tmp - 7;
		}

		tmp = 8 - tmp;
		int DiaSemanaEmpiezaMes = tmp;

		tmp = dia - currentDate.get(currentDate.DAY_OF_WEEK);

		while ((tmp + 7) < TotalDiasMes) {
			tmp = tmp + 7;
		}
		tmp = TotalDiasMes - tmp;

		int DiaSemanaTerminaMes = tmp;

		mes = currentDate.get(currentDate.MONTH);
		meslabel.setText(" " + ARRMES[mes] + " del "
				+ currentDate.get(currentDate.YEAR));
		int j = 1;
		int aux = 0;
		for (int i = 0; i < TotalDiasMes + DiaSemanaEmpiezaMes - 1; i++) {
			aux++;
			if (aux == 8) {
				aux = 1;
				x = 50;
				y = y + 25;
			}
			if (i < DiaSemanaEmpiezaMes - 1) {
				x = x + 56;
			} else {
				auxiliad_dia = String.valueOf(j);
				dia_entero = Integer.parseInt(auxiliad_dia);
				ponerDia(dia_entero, x, y);
				x = x + 56;
				j++;
			}
		}

	}

	public void actionPerformed(ActionEvent ae) {
		int dia;
		int mes;
		int ano;
		if (ae.getSource() == anomenos) {
			reiniciarBotones();
			mes = currentDate.get(currentDate.MONTH);
			dia = currentDate.get(currentDate.DATE);
			ano = currentDate.get(currentDate.YEAR);
			ano = ano - 1;
			currentDate.set(ano, mes, dia);
			anox = String.valueOf(ano);
			mostrar_calendario();
		}
		if (ae.getSource() == anomas) {
			reiniciarBotones();
			mes = currentDate.get(currentDate.MONTH);
			dia = currentDate.get(currentDate.DATE);
			ano = currentDate.get(currentDate.YEAR);
			ano = ano + 1;
			currentDate.set(ano, mes, dia);
			anox = String.valueOf(ano);
			mostrar_calendario();
		}
		if (ae.getSource() == mesmenos) {
			reiniciarBotones();
			mes = currentDate.get(currentDate.MONTH);
			dia = currentDate.get(currentDate.DATE);
			ano = currentDate.get(currentDate.YEAR);
			mes = mes - 1;
			currentDate.set(ano, mes, dia);
			mesx = String.valueOf(mes);
			mostrar_calendario();
		}
		if (ae.getSource() == mesmas) {
			reiniciarBotones();
			mes = currentDate.get(currentDate.MONTH);
			dia = currentDate.get(currentDate.DATE);
			ano = currentDate.get(currentDate.YEAR);
			mes = mes + 1;
			currentDate.set(ano, mes, dia);
			mesx = String.valueOf(mes);
			mostrar_calendario();
		}
	}

	private void ponerDia(int dia_entero, int x, int y) {
		x = x - 32;
		y = y - 30;
		switch (dia_entero) {
		case 1:
			add(dia1);
			dia1.setBounds(x - 10, y - 30, 48, 25);
			dia1.setVisible(true);
			break;
		case 2:
			add(dia2);
			dia2.setBounds(x - 10, y - 30, 48, 25);
			dia2.setVisible(true);
			break;
		case 3:
			add(dia3);
			dia3.setBounds(x - 10, y - 30, 48, 25);
			dia3.setVisible(true);
			break;
		case 4:
			add(dia4);
			dia4.setBounds(x - 10, y - 30, 48, 25);
			dia4.setVisible(true);
			break;
		case 5:
			add(dia5);
			dia5.setBounds(x - 10, y - 30, 48, 25);
			dia5.setVisible(true);
			break;
		case 6:
			add(dia6);
			dia6.setBounds(x - 10, y - 30, 48, 25);
			dia6.setVisible(true);
			break;
		case 7:
			add(dia7);
			dia7.setBounds(x - 10, y - 30, 48, 25);
			dia7.setVisible(true);
			break;
		case 8:
			add(dia8);
			dia8.setBounds(x - 10, y - 30, 48, 25);
			dia8.setVisible(true);
			break;
		case 9:
			add(dia9);
			dia9.setBounds(x - 10, y - 30, 48, 25);
			dia9.setVisible(true);
			break;
		case 10:
			add(dia10);
			dia10.setBounds(x - 10, y - 30, 48, 25);
			dia10.setVisible(true);
			break;
		case 11:
			add(dia11);
			dia11.setBounds(x - 10, y - 30, 48, 25);
			dia11.setVisible(true);
			break;
		case 12:
			add(dia12);
			dia12.setBounds(x - 10, y - 30, 48, 25);
			dia12.setVisible(true);
		case 13:
			add(dia13);
			dia13.setBounds(x - 10, y - 30, 48, 25);
			dia13.setVisible(true);
			break;
		case 14:
			add(dia14);
			dia14.setBounds(x - 10, y - 30, 48, 25);
			dia14.setVisible(true);
			break;
		case 15:
			add(dia15);
			dia15.setBounds(x - 10, y - 30, 48, 25);
			dia15.setVisible(true);
			break;
		case 16:
			add(dia16);
			dia16.setBounds(x - 10, y - 30, 48, 25);
			dia16.setVisible(true);
			break;
		case 17:
			add(dia17);
			dia17.setBounds(x - 10, y - 30, 48, 25);
			dia17.setVisible(true);
			break;
		case 18:
			add(dia18);
			dia18.setBounds(x - 10, y - 30, 48, 25);
			dia18.setVisible(true);
			break;
		case 19:
			add(dia19);
			dia19.setBounds(x - 10, y - 30, 48, 25);
			dia19.setVisible(true);
			break;
		case 20:
			add(dia20);
			dia20.setBounds(x - 10, y - 30, 48, 25);
			dia20.setVisible(true);
			break;
		case 21:
			add(dia21);
			dia21.setBounds(x - 10, y - 30, 48, 25);
			dia21.setVisible(true);
			break;
		case 22:
			add(dia22);
			dia22.setBounds(x - 10, y - 30, 48, 25);
			dia22.setVisible(true);
			break;
		case 23:
			add(dia23);
			dia23.setBounds(x - 10, y - 30, 48, 25);
			dia23.setVisible(true);
			break;
		case 24:
			add(dia24);
			dia24.setBounds(x - 10, y - 30, 48, 25);
			dia24.setVisible(true);
			break;
		case 25:
			add(dia25);
			dia25.setBounds(x - 10, y - 30, 48, 25);
			dia25.setVisible(true);
			break;
		case 26:
			add(dia26);
			dia26.setBounds(x - 10, y - 30, 48, 25);
			dia26.setVisible(true);
			break;
		case 27:
			add(dia27);
			dia27.setBounds(x - 10, y - 30, 48, 25);
			dia27.setVisible(true);
			break;
		case 28:
			add(dia28);
			dia28.setBounds(x - 10, y - 30, 48, 25);
			dia28.setVisible(true);
			break;
		case 29:
			add(dia29);
			dia29.setBounds(x - 10, y - 30, 48, 25);
			dia29.setVisible(true);
			break;
		case 30:
			add(dia30);
			dia30.setBounds(x - 10, y - 30, 48, 25);
			dia30.setVisible(true);
			break;
		case 31:
			add(dia31);
			dia31.setBounds(x - 10, y - 30, 48, 25);
			dia31.setVisible(true);
			break;
		}
	}

	private void reiniciarBotones() {
		dia1.setVisible(false);
		dia2.setVisible(false);
		dia3.setVisible(false);
		dia4.setVisible(false);
		dia5.setVisible(false);
		dia6.setVisible(false);
		dia7.setVisible(false);
		dia8.setVisible(false);
		dia9.setVisible(false);
		dia10.setVisible(false);
		dia11.setVisible(false);
		dia12.setVisible(false);
		dia13.setVisible(false);
		dia14.setVisible(false);
		dia15.setVisible(false);
		dia16.setVisible(false);
		dia17.setVisible(false);
		dia18.setVisible(false);
		dia19.setVisible(false);
		dia20.setVisible(false);
		dia21.setVisible(false);
		dia22.setVisible(false);
		dia23.setVisible(false);
		dia24.setVisible(false);
		dia25.setVisible(false);
		dia26.setVisible(false);
		dia27.setVisible(false);
		dia28.setVisible(false);
		dia29.setVisible(false);
		dia30.setVisible(false);
		dia31.setVisible(false);
	}
}

class calendarioLayout implements LayoutManager {

	public calendarioLayout() {
	}

	public void addLayoutComponent(String name, Component comp) {
	}

	public void removeLayoutComponent(Component comp) {
	}

	public Dimension preferredLayoutSize(Container parent) {
		Dimension dim = new Dimension(0, 0);

		Insets insets = parent.getInsets();
		dim.width = 415 + insets.left + insets.right;
		dim.height = 215 + insets.top + insets.bottom;

		return dim;
	}

	public Dimension minimumLayoutSize(Container parent) {
		Dimension dim = new Dimension(0, 0);
		return dim;
	}

	public void layoutContainer(Container parent) {
		Insets insets = parent.getInsets();
		// Botons per canviar de mes i any i noms dels dies de la setmana
		Component c;
		c = parent.getComponent(0);
		if (c.isVisible()) {
			c.setBounds(insets.left + 10, insets.top + 4, 56, 32);
		}
		c = parent.getComponent(1);
		if (c.isVisible()) {
			c.setBounds(insets.left + 74, insets.top + 4, 56, 32);
		}
		c = parent.getComponent(2);
		if (c.isVisible()) {
			c.setBounds(insets.left + 138, insets.top + 4, 128, 32);
		}
		c = parent.getComponent(3);
		if (c.isVisible()) {
			c.setBounds(insets.left + 274, insets.top + 4, 56, 32);
		}
		c = parent.getComponent(4);
		if (c.isVisible()) {
			c.setBounds(insets.left + 338, insets.top + 4, 56, 32);
		}
		c = parent.getComponent(5);
		if (c.isVisible()) {
			c.setBounds(insets.left + 66, insets.top + 44, 48, 28);
		}
		c = parent.getComponent(6);
		if (c.isVisible()) {
			c.setBounds(insets.left + 122, insets.top + 44, 48, 28);
		}
		c = parent.getComponent(7);
		if (c.isVisible()) {
			c.setBounds(insets.left + 178, insets.top + 44, 48, 28);
		}
		c = parent.getComponent(8);
		if (c.isVisible()) {
			c.setBounds(insets.left + 234, insets.top + 44, 48, 28);
		}
		c = parent.getComponent(9);
		if (c.isVisible()) {
			c.setBounds(insets.left + 290, insets.top + 44, 48, 28);
		}
		c = parent.getComponent(10);
		if (c.isVisible()) {
			c.setBounds(insets.left + 10, insets.top + 44, 48, 28);
		}
		c = parent.getComponent(11);
		if (c.isVisible()) {
			c.setBounds(insets.left + 346, insets.top + 44, 48, 28);
		}
	}
}
